/** 
 * @Title: CrawlOfAutohomeByPost.java 
 * @Package com.zizo.autohome.bbs.crawl 
 * @Description: TODO
 * @author HeYan
 * @date 2016年9月6日 下午5:12:20 
 */
package com.zizo.autohome.bbs.crawl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.zizo.autohome.bbs.dao.BBSDAOOfAutoHome;
import com.zizo.util.ConnectionPool;
import com.zizo.util.DocUtil;
import com.zizo.util.Util;

/**
 * @ClassName: CrawlOfAutohomeByPost
 * @Description: TODO
 * @author HeYan
 * @date 2016年9月6日 下午5:12:20
 * 
 */
public class CrawlOfAutohomeByPost implements Runnable {
	private String id;
	private BBSDAOOfAutoHome dao;

	public CrawlOfAutohomeByPost(String string, BBSDAOOfAutoHome dao) {
		super();
		this.id = string;
		this.dao = dao;
	}

	/**
	 * @Title: getMaxPages
	 * @Description: TODO 获取发布日期大于数据库中的最大日期的页码
	 * @param newDate
	 * @author Alexis
	 * @return
	 */
	public final int getMaxPages() {
		String url = "http://club.autohome.com.cn/bbs/forum-" + id + "-1.html?orderby=dateline&qaType=-1";
		Document doc = DocUtil.getDoc(url);
		String page = null;
		int i = 1;
		try {
			page = doc.select("span.gopage span.fs").first().attr("title").split("共")[1].split("页")[0].trim();
			String sa = id.replaceAll("-", "_");
			String maxDateInDB = dao.getMaxDate("bbs_" + sa);
			int res = 1;
			while (res == 1 && i < Integer.parseInt(page)) {
				url = "http://club.autohome.com.cn/bbs/forum-" + id + "-" + i
						+ ".html?orderby=dateline&qaType=-1#pvareaid=101061";
				doc = DocUtil.getDoc(url);
				String lang = doc.select("div#subcontent dl.list_dl").last().attr("lang");
				String maxDateInWeb = lang.split("\\|")[4];
				res = Util.compare_date(maxDateInWeb, maxDateInDB);
				/* 获得列表页面 */
				i++;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return i;
	}

	public static void main(String[] args) {
		Connection currConn = ConnectionPool.getConnection("autohome_bbs", "localhost", "3306");
		BBSDAOOfAutoHome dao = new BBSDAOOfAutoHome(currConn);
		ExecutorService fixexecutorservice = Executors.newFixedThreadPool(5);
		List<String> carIDs = new ArrayList<String>();
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT allname FROM autohome_bbs.table_name1 WHERE idname<=558 AND idname>=550";
		try {
			st = currConn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				carIDs.add(rs.getString("allname"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println(carIDs.size());
		// List<String> updates = new ArrayList<String>();
		// String sql1 = "SELECT * FROM autohome_bbs.update";
		// try {
		// st = currConn.createStatement();
		// rs = st.executeQuery(sql1);
		// while (rs.next()) {
		// updates.add(rs.getString(1));
		// }
		// } catch (SQLException e) {
		// }
		// carIDs.removeAll(updates);
		// System.out.println(carIDs.size());
		for (String string : carIDs) {
			fixexecutorservice.execute(new CrawlOfAutohomeByPost(string, dao));
		}
		fixexecutorservice.shutdown();
	}

	@Override
	public void run() {
		Map<String, String> syn = dao.getSyn();
		BBSCrawlOfAutoHomeByCycle bbs = new BBSCrawlOfAutoHomeByCycle(id, dao, syn);
		int a = getMaxPages();
		for (int i = 1; i <= a; i++) {
			String url = "http://club.autohome.com.cn/bbs/forum-" + id + "-" + i + ".html";
			Document doc = DocUtil.getDoc(url);
			if (doc == null) {
				return;
			}
			if (doc.select("h1.rtitle").text().contains("帖子被删除（或屏蔽）！")) {
				return;
			}
			Elements el = doc.select("div#subcontent").select("dl.list_dl:not(.bluebg)");
			for (Element element : el) {
				Elements ss = element.select("dt").select("a.a_topic");
				for (Element element2 : ss) {
					String urlagin = element2.attr("href");
					List<String> carids = new ArrayList<String>();
					carids.add(urlagin);
					for (String string : carids) {
						bbs.crawlContent("http://club.autohome.com.cn/" + string);

					}
				}
			}
		}

		System.out.println(id + "已更新");
	}
}
